<template>
  <el-row :gutter="20" class="home">
    <el-col :span="8" style="margin-top: 10px;">
        <el-card class="home-card" shadow="hover">
            <div class="user">
                <img src="../../assets/images/1.png" alt="">
                <div class="userInfo">
                    <p class="name">admin</p>
                    <p class="role">super admin</p>
                </div>
            </div>
            <div class="login-info">
                <p>上次登录时间：<span>2022-7-11</span></p>
                <p>上次登录地点：<span>北京</span></p>
            </div>
        </el-card>
        <el-card shadow="hover" style="margin-top: 20px;">
            <el-table :data="tableData" style="width: 100%">
                <el-table-column prop="name" label="课程"></el-table-column>
                <el-table-column prop="todayBuy" label="今日购买"></el-table-column>
                <el-table-column prop="monthBuy" label="本月购买"></el-table-column>
                <el-table-column prop="totalBuy" label="总购买"></el-table-column>
            </el-table>
        </el-card>
    </el-col>
    <el-col :span="16" style="margin-top: 10px;">
        <div class="num">
            <el-card :body-style="{display:'flex',padding:0}" v-for="item in countData" :key="item.name">
                <component class="icons" :is="item.icon" :style="{background: item.color}"></component>
                <div class="detail">
                    <p class="price">￥{{ item.value }}</p>
                    <p class="desc">{{ item.name }}</p>
                </div>
            </el-card>
        </div>
        <el-card style="height: 225px;">
            <div ref="echarts1" style="height:225px;"></div>
        </el-card>
        <div class="graph">
            <el-card style="height: 205px;">
                <div ref="echarts2" style="height:205px;"></div>
            </el-card>
            <el-card style="height: 205px;">
                <div ref="echarts3" style="height:185px;"></div>
            </el-card>
        </div>
    </el-col>
  </el-row>
</template>

<script>
import axios from 'axios'
import * as echarts from 'echarts';
import { getCurrentInstance, onMounted,reactive,ref } from 'vue'

export default {
    setup(){
        let tableData = ref([])
        let countData = ref([])
        const { proxy } = getCurrentInstance() //proxy相当this
        
        const getTableList = async()=>{
            /* await axios.get("https://www.fastmock.site/mock/70cd50759683ccff554f096e412c53e4/api/home/getTableData").then((res)=>{
                //console.log(res);
                //tableData.value = res.data.data.tableData
                if(res.data.code === 200){
                    tableData.value = res.data.data
                }
            }) */
            let res = await proxy.$api.getTableData()
            //console.log(proxy.$api.getTableData(),'Home.vue');
            tableData.value = res
        }
        const getCountList = async()=>{
            await axios.get("https://www.fastmock.site/mock/70cd50759683ccff554f096e412c53e4/api/home/getCountData").then((res)=>{
                //console.log(res.data.countData);
                countData.value = res.data.countData
            })
        }

        //关于echarts 表格的渲染部分
        let xOptions = reactive({
            //图例文字颜色
            textStyle:{ color:'#333' },
            grid:{ left:'20%' },
            tooltip:{ trigger:'axis' },
            //x轴   
            xAxis: {
                type:'category',  //类目轴
                data: [],
                axisLine:{
                    lineStyle:{ color:'#17b3a3' }
                },
                axisLabel:{ interval:0, color:'#333' }
            },
            yAxis:[
                {
                    type:'value',
                    axisLine:{
                        lineStyle:{ color:'#17b3a3' }
                    }
                }
            ],
            color:["#2ec7c9","#b6a2de","#5ab1ef","#ffb980","#d87a80","#8d98b3"],
            //数据
            series: []
        })
        let pieOptions = reactive({
            tooltip:{ trigger:'item' },
            color:["#0f78f4","#dd536b","#9462e5","#a6a6a6","#e1bb22","#39c362","#3ed1cf"],
            series:[]
        })
        let orderData = reactive({
            xData:[],
            series:[],
        })
        let userData = reactive({
            xData:[],
            series:[],
        })
        let videoData = reactive({
            series:[],
        })
        
        const getChartData = async()=>{
            let res = await proxy.$api.getChartData()
            //console.log(res);
            
            let resOdr = res.otherData
            let resUSer = res.userData
            let resVideo = res.videoData
            orderData.xData = resOdr.date
            const keyArray = Object.keys(resOdr.data[0])
            const series = []
            keyArray.forEach((key)=>{
                series.push({
                    name:key,
                    data:resOdr.data.map((item)=>item[key]),
                    type:'line'
                })
            })
            orderData.series = series
            // console.log(orderData,'****');
            xOptions.xAxis.data = orderData.xData
            xOptions.series = orderData.series
            //console.log(proxy.$refs,'*****');
            let hEcharts = echarts.init(proxy.$refs.echarts1)
            hEcharts.setOption(xOptions)

            userData.xData = resUSer.map(item => item.date)
            userData.series = [
                {
                    name:'新增用户',
                    data: resUSer.map(item => item.new),
                    type:'bar'
                },
                {
                    name:'活跃用户',
                    data: resUSer.map(item => item.active),
                    type:'bar'
                }
            ]
            xOptions.xAxis.data = userData.xData
            xOptions.series = userData.series
            let uEcharts = echarts.init(proxy.$refs.echarts2)
            uEcharts.setOption(xOptions)

            videoData.series = [
                {
                    data:resVideo,
                    type:'pie'
                },
            ]
            pieOptions.series = videoData.series
            let vEcharts = echarts.init(proxy.$refs.echarts3)
            vEcharts.setOption(pieOptions)
        }

        onMounted(()=>{
            getTableList()
            getCountList()
            getChartData()
        })
        
        
        return {
            tableData,
            countData,
        }
    },
    
} 
</script>

<style lang="less" scoped>
.user{
    display: flex;
    align-items: center;
    padding-bottom: 20px;
    margin-bottom: 20px;
    border-bottom: 1px solid #ccc;
    img{
        width: 150px;
        height: 150px;
        border-radius: 50%;
        margin-right: 40px;
    }
    .userinfo{
        .name{
            font-size: 32px;
            margin-bottom: 10px;
        }
        .role{
            color: #999999;
        }
    }
}
.login-info{
    line-height: 28px;
    font-size: 14px;
    color: #999999;
    span{
        color: #666666;
        margin-left: 60px;
    }
}
.num{
    display: flex;
    flex-wrap: wrap;
    justify-content: space-between;
    .el-card{
        width: 32%;
        margin-bottom: 20px;
    }
    .icons{
        width: 65px;
        height: 65px;
        text-align: center;
        line-height: 80px;
        color:#fff;
    }
    .detail{
        display: flex;
        flex-direction: column;
        justify-content: center;
        margin-left: 15px;
        .price{
            font-size: 30px;
            margin-bottom: 10px;
            height: 30px;
            line-height: 30px;
        }
        .desc{
            font-size: 14px;
            color: #999;
            text-align: center;
        }
    }
}
.graph{
    display: flex;
    justify-content: space-between;
    margin-top: 10px;
    .el-card{
        width: 48%;
    }
}
</style>